﻿#if ANDROID
using Android.Content;
using Android.Provider;
using Android.Net;
using Java.IO;
using Microsoft.Maui.Storage;

namespace WaterFlow
{
    public static class AndroidSaveHelper
    {
        public static async Task<bool> SaveToGallery(byte[] imageData, string fileName)
        {
            try
            {
                var context = Platform.CurrentActivity;
                ContentValues values = new ContentValues();
                values.Put(MediaStore.IMediaColumns.DisplayName, fileName);
                values.Put(MediaStore.IMediaColumns.MimeType, "image/jpeg");
                values.Put(MediaStore.IMediaColumns.RelativePath, Android.OS.Environment.DirectoryPictures);

                Android.Net.Uri uri = context.ContentResolver.Insert(MediaStore.Images.Media.ExternalContentUri, values);
                using var stream = new MemoryStream(imageData);
                using var os = context.ContentResolver.OpenOutputStream(uri);
                await stream.CopyToAsync(os);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
#endif